package.cpath = "luaclib/?.so;3rd/lua-cjson/?.so;"
local skynet = require "skynet"
local mongo = require "mongo"
local mysql = require "mysql"
local json   = require "cjson"
require "skynet.manager"	-- import skynet.register
require "config_redis"
local httpc = require "http.httpc"	
local dns = require "dns"

local LOGIN_AUTH = false

local mysql_db
local function start_mysql(args)
	local function on_connect(db)
		db:query('set names'..args.charset);
	end
	mysql_db=mysql.connect{
		host	= args.host,
		port	= args.port,
		database= args.database,
		user	= args.user,
		password= args.password,
		max_packet_size = 1024 * 1024,
		on_connect = on_connect,
	}
end

local function third_login_auth(account, token)
    return true
end

CMD = {}

-- 登陆验证
function CMD.login_auth( args )
    local account = args.account
    local uid = args.uid
    local token = args.token

    local ok = false
    -- 第三方账号验证
    if LOGIN_AUTH == true then
        ok = third_login_auth(account, token)
        return ok
    end

    ok = true
    return ok
end


-- 微信登录
function CMD.login_auth_wechat(args)
	local code = args.code
	local token = args.token
	local openid = args.openid
	local respheader = {}
	local form = {code = code,token=token,openid=openid}
	local status, body = httpc.post(login_url, '/admin_mj/wechat.php',form, respheader);
	if status == 200 then
		local rearr = json.decode(body)
		if rearr.ret == 0 then
			return rearr.info,rearr.expires_time,rearr.password
		else
			return 2
		end
	else
		return 1
	end
end

-- 获取 支付订单号
function CMD.get_pay_id(args) 
	local respheader = {}
	local form = {uid = args.uid,waresid=args.waresid,price=args.price}
	local status, body = httpc.post(login_url,'/admin_mj/pay/pay.php',form, respheader);
	if status == 200 then 
		local rearr = json.decode(body) 
        	if rearr.prepayid ~= nil then
                	return rearr
        	end 
	end
	return ''
end

-- 根据账号查找uid
function CMD.login_get_uid(args,typ)
    local account = args.account
	local password = nil
	local query_sql
	if typ == 2 then
		password = args.password
		query_sql = string.format("select * from user where account = '%s' and password = '%s'",account,password);
	else
		query_sql = string.format("select * from user where account = '%s'",account);
	end
	local res = mysql_db:query(query_sql);
	if #res == 0 then
		return nil,0,0
	else
		local expires_time = 0
		if res[1]['channel']  then 
			expires_time = res[1]['expires_time']
		end
		if expires_time == 0 then 
			expires_time = os.time() + 86400
		end
		return res[1]['uid'],res[1]['status'],expires_time
	end
end

-- 账号注册uid
function CMD.login_register(args)
    local ok = false
    local msg = ""
    local account = args.account
	--account,name,sex,head,ip,channel,password,expires_time)
	local result= skynet.call("MG_SAVE", "lua", "insert_user", account, account, 1, '', '','self',args.password,0);
	
	if result then
		ok = true	
		uid = result
	else
		ok = false	
		uid = 0
	end
    return ok, uid
end

skynet.start(function()

	httpc.dns()	-- set dns server
	
    start_mysql(mysql_args)
	skynet.dispatch("lua", function(_,_, command, ...)
		local f = CMD[command]
		skynet.ret(skynet.pack(f(...)))
	end)
	skynet.register "LOGIN"
end)

